﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class Login : Form
    {
        private int _close_flag;
        private bool _check_connect = false;
        private LoginDao loginDao;
        public static string userName;
        public Login()
        {
            _close_flag = 0;
            loginDao = new LoginDao();
            _check_connect = loginDao.checkConnect();
            InitializeComponent();
            try
            {
                this.Closing += new System.ComponentModel.CancelEventHandler(this.CloseAppLication);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void CloseAppLication(Object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_close_flag == 0)
            {
                Application.Exit();
            }
        }
        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (!_check_connect)
            {
                Error f = new Error(UserConstant.CONNECTION);
                f.ShowDialog();
                f.Dispose();
            }
            else
            {
                if (loginDao.login(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
                {
                    _close_flag = 1;
                    userName = txtUserName.Text.Trim();
                    this.Close();
                }
                else
                {
                    Error f = new Error(UserConstant.LOGIN_FAIL);
                    f.ShowDialog();
                    f.Dispose();
                    txtPassword.Text = "";
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
